import { wxRequest } from '../../../utils/server.js';
const _wxRequest = new wxRequest();

const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    address: '',
    regionItem: {},
    cententDetail: {},
    isDisable: false,
    isScan: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var item = wx.getStorageSync('angtItem')
    if (item) {
      console.log(item);
      this.setData({
        cententDetail: item,
        regionItem: {
          id: item.regId,
          regName: item.regName || '请选择区域经理'
        }
      })
      console.log(item.regId);
      this.findRegionList(item.regId);
    }
  },

  onUnload: function () {
    console.log('代理商')
    wx.removeStorageSync('angtItem')
  },

  handleRegion() {
    //选择区域经理
    wx.navigateTo({
      url: '/pages/publish/template/region',
    })
  },

  // 获取区域经理列表
  findRegionList: function (regId) {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + "/org/protected/bizOrg/findRegionList",
      method: "post",
      data: { }
    },  (res)=>{
      var list = res.list
      let index = list.findIndex(item => item.id == regId)
      console.log(index);
      if (index != -1) {
        this.setData({
          regionItem: {
            id: list[index].id,
            regName: list[index].regName || '请选择区域经理'
          }
        })
      }
    })
  },

  onHide: function () {
    wx.removeStorageSync('angtItem')
  },

  addressDetail: function (e) {
    this.setData({
      address: e.detail
    })
  },

  scanQrCode: function(e) {
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ['qrCode'],
      success: async (res) => {
        console.log("扫描后的代理商id：",res.result);
        this.findAgentDetail(res.result)
      }
    })
  },

  findAgentDetail: function(paramsId) {
    wx.request({
      method: 'post',
      url: app.globalData.apiUrl + "/org/bizOrg/getAgentById?id=" + paramsId,
      success: (res) => {
        if(!res.data){
          wx.showToast({
            icon: 'none',
            title: paramsId+':不存在',
          })
        }
        console.log("请求代理数据",res)
        this.setData({
          cententDetail: res.data,
          isDisable: true,
          isScan: true,
        })
      }
    })
  },

  createRegionAgent() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + "/org/protected/bizOrg/createRegionAgent",
      method: 'post',
      data: {
        ageId: this.data.cententDetail.id,
        regId: this.data.regionItem.id,
        ageType: 2
      }
    }, (res) => {
      wx.showToast({
        title: '发布成功',
      })
    })
  },

  formSubmit: function (e) {
    if(this.data.isScan) {
      this.createRegionAgent();
    }else{
      var formdata = e.detail.value;
      var address = this.data.address
      if (address && address.length > 0) {
        formdata.prov = address[0].name
        formdata.proId = address[0].code
        formdata.city = address[1].name
        formdata.cityId = address[1].code
        formdata.disName = address[2].name
        formdata.disId = address[2].code
      }
  
      if (formdata.ageName == '') {
        wx.showToast({
          icon: 'none',
          title: '供应商名称不能为空',
        })
        return 
      }
  
      if (formdata.detailAddress == '') {
        wx.showToast({
          icon: 'none',
          title: '详细地址不能为空',
        })
        return 
      }
  
      if (formdata.disId == '' || formdata.disName == '') {
        wx.showToast({
          icon: 'none',
          title: '请选择所在地区',
        })
        return 
      }
  
      if (formdata.director == '') {
        wx.showToast({
          icon: 'none',
          title: '负责人名称不能为空',
        })
        return 
      }
  
      if (formdata.mobile == '') {
        wx.showToast({
          icon: 'none',
          title: '手机号码不能为空',
        })
        return
      }
  
      if (formdata.mobile.length != 11 && formdata.mobile.length != 8 && formdata.mobile.length != 10) {
        wx.showToast({
          icon: 'none',
          title: '手机号码格式不正确',
        })
        return
      }

      if (formdata.mobile.length == '10' && formdata.mobile.slice(0, 2) != '09') {
        // 台湾手机号 09开头+任意8位数
        wx.showToast({
          icon: 'none',
          title: '台湾手机号码格式不正确',
        })
        return
      }

      formdata.regId = this.data.regionItem.id;
      var url = '/org/protected/bizOrg/createAgent';
      var title = "发布成功"
      //修改
      if (this.data.cententDetail && this.data.cententDetail.id) {
        url = "/org/protected/bizOrg/updateAgent";
        formdata.id = this.data.cententDetail.ageId;
        title = "修改成功"
      }  
  
  
      //请求
      _wxRequest.wxTonkenAjax({
        url: app.globalData.apiUrl + url ,
        method: 'POST',
        data: formdata
      }, (res) => {
        console.log(res)
        if (res.flag) {
          wx.showToast({
            title,
          })
          setTimeout(() => {
            wx.redirectTo({
              url: '/pages/publish/agent/list',
            })
          }, 1500)
        }
      })
    }
    
  },


  goback: function () {
    wx.navigateBack({})
  }


})